package com.lebang.model;

import com.alibaba.fastjson.JSON;
import com.lebang.common.Util;
import com.lebang.entity.RequestHeaders;
import com.lebangweb.common.UUIDUtil;
import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * @Author: hwq
 * @Date: 2020/4/5 19:05
 * @Version 1.0
 */
@Data
@Document("error_log")
public class ErrorLogModel {
    /**
     * 请求地址
     */
    private String uri;
    /**
     * 请求参数
     */
    private String params;
    private String appId;
    private String appStore;
    private String brand;
    private String imei;
    private String macAddr;
    private String os;
    private String osv;
    private long time;
    private String memberId;
    private String version;
    private String ipAddr;
    private String ua;
    private String ex;
    private String className;
    private LocalDateTime createTime;
    public ErrorLogModel() {
    }

    public ErrorLogModel(HttpServletRequest request) {
        RequestHeaders requestHeaders = new RequestHeaders(request);
        this.appId = requestHeaders.getAppid() == null ? "xianyu" : requestHeaders.getAppid();
        this.appStore = requestHeaders.getAppStore();
        this.version = requestHeaders.getVersion();
        this.imei = requestHeaders.getImei()== null ? "NoneImei:"+ UUIDUtil.NS() : requestHeaders.getImei();
        this.brand = requestHeaders.getBrand();
        this.os = requestHeaders.getOs();
        this.osv = requestHeaders.getOsv();
        this.macAddr = requestHeaders.getMacAddr();
        this.memberId = requestHeaders.getUserid();
        this.time = System.currentTimeMillis();
        this.ipAddr = Util.getIpAddress(request);
        this.params = JSON.toJSONString(request.getParameterMap());
        this.ua = requestHeaders.getUa() == null ? "" : requestHeaders.getUa();
        createTime=LocalDateTime.now().plusHours(8);
    }
}
